#

| Chapter 21 More About Managing Files |

## 21.12 Copying Files to a Directory

Several commands like cp and mv will let you copy a file to a directory. That is, given the command:

>     % cp file1 somewhere

if somewhere is a directory, cp copies file1 into the directory, leaving its name unchanged. You get a new file whose relative pathname (1.21) is somewhere/file1. A few commands, for example, mv and ln, have the same behavior.

Of course, this version of the command looks the same as a "regular" cp command. This leads to a common frustration: what if the directory somewhere doesn't exist? Maybe you forgot to create it; maybe you misspelled the name. cp doesn't know that you really meant a directory, so it just copies file1 into a new file, somewhere, in the current directory. There are plenty of situations in which this can be plenty confusing, and even (if you're unlucky) lead to errors.

There's an easy safeguard, though. If you're copying files into a directory, add a slash and dot (`/.`) after the directory's pathname:

>     % cp file1 path-to-directory/.

This makes a pathname to the special entry named `.` (dot) (1.21, 18.2) in the directory somewhere-which is a link to the directory itself. If the directory named somewhere doesn't exist, you'll get an error message:

>     % cp file1 somewhere/.
>     cp: somewhere/.: No such file or directory

- ML

21.11 Safer Removing, Moving, and Copying |  | 21.13 Read an Inode with stat